package cn.akira.domain.strategy.service.rule.tree.impl;

import cn.akira.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import cn.akira.domain.strategy.service.rule.tree.ILogicTreeNode;
import cn.akira.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import cn.akira.types.common.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author fjl95
 * @date 2025/5/16
 * @description 幸运奖节点
 */
@Slf4j
@Component("rule_luck_award")
public class RuleLuckAwardLogicTreeNode implements ILogicTreeNode {

    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {
        log.info("规则过滤-兜底奖品 userId:{} strategyId:{} awardId:{} ruleValue:{}", userId, strategyId, awardId,ruleValue);
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        if (splitRuleValue.length == 0){
            log.error("规则过滤-兜底奖品，兜底奖品未配置 userId:{} strategyId:{} awardId:{} ruleValue:{}", userId, strategyId, awardId,ruleValue);
            throw new RuntimeException("兜底奖品未配置" + ruleValue);
        }
        //获得兜底奖励
        Integer luckAwardId = Integer.parseInt(splitRuleValue[0]);
        String awardRuleValue = splitRuleValue.length > 1 ? splitRuleValue[1] : null;

        //兜底奖励只有TAKE_OVER
        return DefaultTreeFactory.TreeActionEntity.builder()
                .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                        .awardId(luckAwardId)
                        .awardRuleValue(awardRuleValue)
                        .build())
                .build();
    }
}
